local GetTime = GetTime();
--
local start, duration = GetSpellCooldown("Pulverize");
local gcd = start + duration;
local function GetCooldown(id)
  local start, duration = GetSpellCooldown(id);
  local cd = start + duration - gcd;
  if cd < 0 then return 0 end
  return cd
end

-- by Lars: checks if 10% physical damage reduced debuff is on target, returns the name of the spell, time of start and duration
-- and saves some lines of code.
local physDmgReduce={"Demoralizing Roar", "Demoralizing Shout", "Curse of Weakness", "Scarlet Fever", "Vindication"};
local physDmgReduceMinDuration = 6;
local function PhysDmgReducedInfo()
  for idx=1,#physDmgReduce do
    local _, _, start, duration = IconAura("HARMFUL", "target", physDmgReduce[idx]);
    if(duration) then
      if (duration+start-GetTime>physDmgReduceMinDuration) then
        return false;
      end
    end
  end
  return true;
end

if (not (UnitExists("target") and UnitCanAttack("player", "target"))) or (UnitIsDeadOrGhost("target")) then
  return
end

--1-) Check my stance. If I'm not in bear form, I should switch to it.
if (GetBonusBarOffset() ~= 3) then
  return IconSpell("Bear Form");
end

if (PhysDmgReducedInfo()) then
  return IconSpell ("Demoralizing Roar");
end

if (UnitPower("player")<15 and GetCooldown("Enrage")<= 0) then
  return IconSpell("Enrage");
end

if (GetCooldown("Barkskin") <= 0) then
  return IconSpell("Barkskin");
end
